#include<iostream>
#include<queue>

using namespace std;

const int N = 1e5 + 10;
int q[N];
int n , h , t;
int main()
{
    cin >> n;
    while(n--)
    {
        int op; cin >> op;
        if(op == 1)
        {
            int x;
            cin >> x;
            q[++t] = x;
        }
        else if(op == 2)
        {
            if(h == t) cout << "ERR_CANNOT_POP" << endl;
            else h++;
        }
        else if(op == 3)
        {
            if(t == h) cout << "ERR_CANNOT_QUERY" << endl;
            else cout << q[h + 1] << endl;
        }
        else
        {
            cout << t - h << endl;
        }
    }
    return 0;
}